﻿' ------------------------------------------------------------------------------
' This form presents the user with three buttons used for database maintenance.
' The instructional text preceeding each button is read from textfile-based
' resources to simplify authoring.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Public Class DatabaseToolsForm
    Inherits System.Windows.Forms.Form

    Shared m_form As DatabaseToolsForm = Nothing

    Public Shared Sub ShowForm()
        If m_form Is Nothing Then
            m_form = New DatabaseToolsForm
        End If
        m_form.Show()
        m_form.BringToFront()
    End Sub

    Private Sub DatabaseToolsForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        pnlCreateDatabase.Visible = False
        txtCheckDatabase.Text = My.Resources.InstructionsCheckDatabase.Replace(vbCrLf, " ")
        txtCreateDatabase.Text = My.Resources.InstructionsCreateDatabase.Replace(vbCrLf, " ")
        txtLoadSamples.Text = My.Resources.InstructionsLoadSamples.Replace(vbCrLf, " ")
    End Sub


    Private Sub btnCheckDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheckDatabase.Click
        Dim db As New SpatialAlbum.Core.Database
        db.CheckDatabase(g_connection)
    End Sub

    Private Sub btnCreateDatabase_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCreateDatabase.Click
        Dim rc As DialogResult = MessageBox.Show( _
            "This will create a new database. Any data in the current database will be lost." & vbCrLf & vbCrLf & _
            "Do you want to create a new database?", _
            "Create Database", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
        If rc <> Windows.Forms.DialogResult.Yes Then Return

        Me.Cursor = Cursors.WaitCursor
        pnlCreateDatabase.Visible = True
        pnlCreateDatabase.BringToFront()
        Try
            g_logwindow.UpdateFrequently = True
            Dim db As New SpatialAlbum.Core.Database
            db.CreateDatabase(True, cbxKeepAlbumData.Checked, g_connection)
            MessageBox.Show("Database created.", "Create Database", MessageBoxButtons.OK, MessageBoxIcon.Information)
        Catch ex As Exception
            Throw ex
        Finally
            pnlCreateDatabase.Visible = False
            Me.Cursor = Cursors.Default
            g_logwindow.UpdateFrequently = False
        End Try
    End Sub

    Private Sub btnLoadSamples_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadSamples.Click
        g_imageloader.AddImage(IO.Path.Combine(IO.Directory.GetCurrentDirectory(), My.Settings.RTS_TestPhoto1), g_connection)
        g_imageloader.AddImage(IO.Path.Combine(IO.Directory.GetCurrentDirectory(), My.Settings.RTS_TestPhoto2), g_connection)
        g_imageloader.AddImage(IO.Path.Combine(IO.Directory.GetCurrentDirectory(), My.Settings.RTS_TestPhoto3), g_connection)
        g_loaderform.Show()
    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub DatabaseToolsForm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Me.Hide()
        e.Cancel = True
    End Sub

    Private Sub btnClearPhotos_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClearPhotos.Click
        Dim db As New SpatialAlbum.Core.Database
        db.ClearPhotoTable(g_connection)
    End Sub
End Class